<template>
  <div class="main-content">
    <el-row>
      <el-button
        size="medium"
        class="filter-item"
        type="primary"
        @click="handleCreate"
        >新增员工信息</el-button
      >
      <el-button
        size="medium"
        class="filter-item"
        type="primary"
        @click="importComfirm"
        >导入员工信息</el-button
      >
      <el-select
        clearable
        v-model="getListData.valid"
        placeholder="选择状态"
        size="medium"
        class="filter-item"
        @change="getList"
        style="margin-left: 20px"
      >
        <el-option label="已停用" value="0"> </el-option>
        <el-option label="已启用" value="1"> </el-option>
      </el-select>

        <el-input
            v-model="getListData.input"
            placeholder="请输入账号,姓名或者邮箱"
            clearable
            style="width: 250px;margin-left:10px;"
        class="filter-item" 
        size="medium"
          ></el-input>


            <el-button
        size="medium"
        class="filter-item"
        type="primary"
        @click="getList"
          style="margin-left:10px;"
        >查询</el-button
      >




    </el-row>

    <p class="maintitle">员工信息列表</p>
    <el-table :data="tableData" border>
      <el-table-column type="selection" width="50"> </el-table-column>
      <el-table-column type="index" label="ID" align="center">
      </el-table-column>
      <el-table-column
        prop="head_img"
        label="员工照片"
        width="150"
        align="center"
      >
        <template slot-scope="{ row }">
          <el-image
            style="width: 98px; height: 60px"
            :src="baseurl + '/' + row.head_img"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="usercode"
        label="系统账号"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="password"
        label="密码"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="username"
        label="员工姓名"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="email" label="电子邮件" width="150" align="center">
      </el-table-column>
      <el-table-column prop="mobile" label="手机号" width="150" align="center">
      </el-table-column>
      <el-table-column
        prop="department"
        label="在职总部门"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="organization"
        label="在职部门组织"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="position"
        label="在职职位"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="validInfo"
        label="员工状态"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="ascription_name"
        label="归属业务组长"
        width="150"
        align="center"
      >
      </el-table-column>
       <el-table-column
        prop="manager_name"
        label="归属业务经理"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="role_name" label="角色" width="150" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" @click="handleEdit(row)"
            >编辑</el-button
          >
          <el-popconfirm
            v-if="row.valid == 0"
            confirm-button-text="确认"
            cancel-button-text="取消"
            title="确认删除当前员工？"
            @confirm="deleteRow(row.id)"
            style="margin-left: 10px"
          >
            <el-button size="mini" type="text" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      style="text-align: right"
      v-show="totalPage > 0"
      :total="totalPage"
      :page.sync="getListData.pageNo"
      :limit.sync="getListData.pageSize"
      @pagination="getList"
    ></pagination>

    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogVisible"
      width="100"
    >
      <el-form
        :model="form"
        ref="acForm"
        label-position="left"
        label-width="150px"
        :inline="true"
      >
        <el-form-item
          label="账号"
          prop="usercode"
          :rules="[{ required: true, message: '账号不能为空' }]"
        >
          <el-input
            clearable
            v-model="form.usercode"
            :disabled="dialogStatus === 'update'"
          ></el-input>
        </el-form-item>
         <el-form-item
          label="密码"
          prop="password"
          :rules="[{ required: true, message: '账号不能为空' }]"
        >
          <el-input
            clearable
            v-model="form.password"
  
          ></el-input>
        </el-form-item>
        <el-form-item
          label="姓名"
          prop="username"
          :rules="[{ required: true, message: '姓名不能为空' }]"
        >
          <el-input clearable v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item
          label="电子邮件"
          prop="email"
          :rules="[{ required: true, message: '电子邮件不能为空' }]"
        >
          <el-input clearable v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item
          label="手机号"
          prop="mobile"
          :rules="[{ required: true, message: '手机号不能为空' }]"
        >
          <el-input clearable v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item
          label="在职总部门"
          prop="department"
          :rules="[{ required: true, message: '在职总部门不能为空' }]"
        >
          <el-select
            v-model="form.department"
            placeholder="请选择"
            @change="orgList"
          >
            <el-option
              v-for="item in dptmt"
              :key="item.dptmt_name"
              :label="item.dptmt_name"
              :value="item.dptmt_name"
            >
            </el-option>
          </el-select>

          <!-- <el-input clearable v-model="form.department"></el-input> -->
        </el-form-item>
        <el-form-item
          label="在职部门组织"
          prop="organization"
          :rules="[{ required: true, message: '在职部门组织不能为空' }]"
        >
          <el-select v-model="form.organization" placeholder="请选择">
            <el-option
              v-for="item in org"
              :key="item.org_name"
              :label="item.org_name"
              :value="item.org_name"
            >
            </el-option>
          </el-select>
          <!-- <el-input clearable v-model="form.organization"></el-input> -->
        </el-form-item>
        <el-form-item
          label="在职职位"
          prop="position"
          :rules="[{ required: true, message: '在职职位不能为空' }]"
        >
          <el-input clearable v-model="form.position"></el-input>
        </el-form-item>
        <el-form-item
          label="员工状态"
          prop="valid"
          :rules="[{ required: true, message: '员工状态不能为空' }]"
        >
          <el-select v-model="form.valid" placeholder="请选择">
            <el-option label="启用" value="1"> </el-option>
            <el-option label="停用" value="0"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="角色"
          prop="role_id"
          :rules="[{ required: true, message: '角色不能为空' }]"
        >
          <el-select
            v-model="form.role_id"
            placeholder="请选择"
            @change="changeRole"
          >
            <el-option label="超级管理员" value="1"> </el-option>

            <el-option label="业务员" value="3"> </el-option>
            <el-option label="业务组长" value="4"> </el-option>
            <el-option label="业务经理" value="5"> </el-option>
            <el-option label="市场部经理" value="6"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="form.role_id == 3 || form.role_id == 6"
          label="归属业务组长"
          prop="ascription"
          :rules="[{ required: true, message: '归属业务组长不能为空' }]"
        >
          <el-select
            v-model="form.ascription_name"
            filterable
            remote
            reserve-keyword
            placeholder="请输入业务主管"
            :remote-method="getAscription"
            :loading="loading1"
            @change="changeAscription"
            value-key="id"
          >
            <el-option
              v-for="item in ascriptions"
              :key="item.id"
              :label="item.username"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>






         <el-form-item
          v-if="form.role_id != 5 &&  form.role_id != ''"
          label="归属业务经理"
          prop="manager"
          :rules="[{ required: true, message: '业务经理不能为空' }]"
        >
          <el-select
            v-model="form.manager_name"
            filterable
            remote
            reserve-keyword
            placeholder="请输入业务经理"
            :remote-method="getManager"
            :loading="loading2"
            @change="changeManager"
            value-key="id"
          >
            <el-option
              v-for="item in Manager"
              :key="item.id"
              :label="item.username"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="dialogStatus === 'create' ? updateData('') : updateData()"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 导入 -->
    <el-dialog
      title="导入"
      v-loading.fullscreen.lock="loading"
      element-loading-text="正在导入,请稍后..."
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      :visible.sync="importVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-button
        size="medium"
        type="success"
        style="margin-bottom: 10px"
        @click="templateDownload"
        >模板下载</el-button
      >
      <el-upload
        class="upload-demo"
        drag
        action="#"
        :multiple="false"
        :limit="1"
        :auto-upload="false"
        ref="upload"
        :http-request="importPattern"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">
          *只能上传一个文件，若需修改请删除原文件后重新上传
        </div>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button @click="importVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmImport">导入</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Message } from "element-ui";
import {
  staffList,
  staffImport,
  staffEdit,
  searchAscription,
  staffDelete,
  dptmtList,
  orgList,
} from "@/app/api/system";
import { mapState } from "vuex";
import Pagination from "@/app/components/Pagination";

export default {
  components: {
    Pagination,
  },
  name: "Staff",
  data() {
    return {
      loading1: false,
      loading2: false,

      ascriptions: [],
      Manager:[],
      importVisible: false,
      loading: false,
      tableData: [],
      totalPage: 1,
      listLoading: true,
      getListData: {
        pageNo: 1,
        pageSize: 10,
        input:""
      },
      dialogVisible: false,
      form: {},
      dialogStatus: "", //编辑或新增
      textMap: {
        update: "编辑",
        create: "新增",
      },
      baseurl: api_url,
      dptmt: [],
      org: [],
    };
  },
  computed: {
    ...mapState("user", ["session", "account"]),
  },
  created() {
    this.getList();
    this.dptmtList();
  },
  methods: {
    dptmtList() {
      dptmtList()
        .then((res) => {
          this.dptmt = res.data;

          console.log(this.dptmt);
        })
        .catch((error) => {
          console.log("dptmtList error", error);
        });
    },

    orgList() {
      orgList({ dptmt_name: this.form.department })
        .then((res) => {
          this.org = res.data;
        })
        .catch((error) => {
          console.log("orgList error", error);
        });
    },
    changeRole(val) {
      if (val == 4) {
        this.form.ascription = "";
        this.form.ascription_name = "";
      }
    },
    changeAscription(ascription) {
      this.form.ascription = ascription.id;
      this.form.ascription_name = ascription.username;
    },
      changeManager(manager) {
      this.form.manager = manager.id;
      this.form.manager_name = manager.username;
    },
    
    getAscription(query) {
      if (query !== "") {
        this.loading1 = true;
        searchAscription({ role: 4, username: query })
          .then((res) => {
            console.log(res);
            this.ascriptions = res.data;
            this.loading1 = false;
          })
          .catch((error) => {
            console.log("ascriptionList error", error);
            this.loading1 = false;
          });
      }
    },

    getManager(query) {
      if (query !== "") {
        this.loading2 = true;
        searchAscription({ role: 5, username: query })
          .then((res) => {
            console.log(res);
            this.Manager = res.data;
            this.loading2 = false;
          })
          .catch((error) => {
            console.log("ascriptionList error", error);
            this.loading2 = false;
          });
      }
    },
    importComfirm() {
      this.importVisible = true;
      this.$nextTick(() => {
        this.$refs["upload"].clearFiles();
      });
    },
    confirmImport() {
      this.loading = true;
      this.$nextTick(() => {
        this.$refs.upload.submit();
      });
    },
    importPattern(param) {
      const fileObj = param.file;
      const form = new FormData();
      form.append("file", fileObj);
      staffImport(form)
        .then((res) => {
          console.log("import_score res", res);
          Message({
            message: "导入成功",
            type: "success",
            duration: 2 * 1000,
          });
          this.loading = false;
          this.importVisible = false;
          this.getList();
        })
        .catch((error) => {
          console.log("import_score error", error);
          this.loading = false;
          this.importVisible = false;
        });
    },
    templateDownload() {
      window.location.href = this.baseurl + "/template/staff.xlsx";
    },
    getList() {
      staffList(this.getListData)
        .then((res) => {
          this.tableData = res.data.ret;
          this.totalPage = res.data.count;
          this.listLoading = false;
        })
        .catch((error) => {
          console.log("studentSourceList error", error);
        });
    },
    deleteRow(aid) {
      staffDelete({ aid })
        .then((res) => {
          if (res.code == "00000") {
            Message({
              message: "删除成功",
              type: "success",
              duration: 5 * 1000,
            });
            this.getList();
          }
        })
        .catch((error) => {
          console.log("save error", error);
        });
    },
    handleCreate() {
      this.form = {
        usercode: "",
        username: "",
        email: "",
        mobile: "",
        department: "",
        organization: "",
        position: "",
        valid: "",
        role_id: "",
        ascription: "",
      };
      this.dialogStatus = "create";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["acForm"].clearValidate();
      });
    },
    updateData(id) {
      this.$refs["acForm"].validate((valid) => {
        if (valid) {
          if (id) {
            this.form.id = id;
          }
          staffEdit(this.form)
            .then((res) => {
              if (res.code == "00000") {
                Message({
                  message: "操作成功",
                  type: "success",
                  duration: 2 * 1000,
                });
                this.getList();
                this.dialogVisible = false;
              }
            })
            .catch((error) => {
              console.log("editOrder error", error);
            });
        } else {
          return false;
        }
      });
    },
    handleEdit(row) {
      this.form = Object.assign({}, this.form, row);
      console.log(this.form);
      this.dialogStatus = "update";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["acForm"].clearValidate();
      });
    },
  },
};
</script>

<style>
.main-content {
  /* width: 97%; */
  height: 840px;
  margin: 1% auto;
  border-radius: 10px;
  box-shadow: 7px 7px 10px 0px rgba(0, 0, 0, 0.25);
  background-color: #fbfbfb;
  padding: 0px;
  position: relative;
}
.maintitle {
  font-family: Roboto;
  font-style: normal;
  font-weight: 500;
  font-size: 20px;
  line-height: 22px;
  color: #000000;
  margin: 10px 0 20px 10px;
}
</style>
